﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessLayer.Tables;
using System.Data.SqlClient;

public partial class Promosyon_Promosyon : ChildPage
{
    #region Kayıt Ekle
    private void Kaydet()
    {
        header = new PromoHeader();
        details = new PromoDetails();
        if (txtPromosyonAdi.Text != "")
            header.Adi = txtPromosyonAdi.Text;
        if (txtAlisverisLimiti.Text != "")
            header.AlisverisLimit = decimal.Parse(txtAlisverisLimiti.Text);
        if (drpPromosyonTip.SelectedIndex != -1)
            header.Cinsi = int.Parse(drpPromosyonTip.SelectedItem.Value);
        header.Create_Date = DateTime.Now;
        header.Create_User = 1;
        header.Hidden = true;
        header.Locked = true;
        header.Update_Date = DateTime.Now;
        header.Update_User = 1;
        if (drpIndirimTipi.SelectedIndex != -1)
            header.Tip = Int16.Parse(drpIndirimTipi.SelectedItem.Value);
        if (txtIndirimOrani.Text != "")
            header.IndirimOrani = decimal.Parse(txtIndirimOrani.Text);
        if (txtIndirimTutari.Text != "")
            header.IndirimTutari = decimal.Parse(txtIndirimTutari.Text);
        if (txtPromosyonKod.Text != "")
            header.Kod = txtPromosyonKod.Text;
        if (drpOdemeTipi.SelectedIndex != -1)
            header.OdemeTipi = Int16.Parse(drpOdemeTipi.SelectedItem.Value);
        if (txtPromosyonAdeti.Text != "")
            header.PromoAdet = decimal.Parse(txtPromosyonAdeti.Text);
        if (txtUrunFiyat.Text != "")
            header.PromoFiyat = decimal.Parse(txtUrunFiyat.Text);
        if (txtUrunLimit.Text != "")
            header.UrunLimit = decimal.Parse(txtUrunLimit.Text);
        if (txtVerilecekUrun.Text != "")
            header.PromoUrun = txtVerilecekUrun.Text;
        if (txtBaslangicTarihi.Text != "")
            header.BaslangicTarihi = DateTime.Parse(txtBaslangicTarihi.Text);
        if (txtBitisTarihi.Text != "")
            header.BitisTarihi = DateTime.Parse(txtBitisTarihi.Text);
        if (drpMstIndOran.Enabled == true)
        {
            if (drpMstIndOran.SelectedItem.Value == "0")
                header.MusIndDahil = true;
            else
                header.MusIndDahil = false;
        }
        header.Save();
        details.Create_Date = DateTime.Now;
        details.Create_User = 1;
        details.Hidden = true;
        details.Locked = true;
        details.Update_Date = DateTime.Now;
        details.Update_User = 1;
        details.Tipi = 0;
        details.Kod = txtPromosyonKod.Text;
        if (drpIndirimTipi.SelectedItem.Value == "1")
        {
            if (txtList.Text != "")
            {
                details.KayitSil("DELETE FROM PROMODETAILS WHERE (prod_kod = N'" + details.Kod + "') AND (prod_tip = 0)");
                string[] diziCari = txtList.Text.Split(char.Parse(";"));
                for (int i = 0; i < diziCari.Length; i++)
                {
                    details.Satir = i;
                    details.PromoKod = diziCari[i].ToString();
                    details.Save();
                }
            }
            else
                details.KayitSil("DELETE FROM PROMODETAILS WHERE (prod_kod = N'" + details.Kod + "') AND (prod_tip = 0)");
        }
        details.Tipi = 1;
        if ((drpPromosyonTip.SelectedItem.Value == "1") || (drpPromosyonTip.SelectedItem.Value == "2") || (drpPromosyonTip.SelectedItem.Value == "3"))
        {
            if (txtListStok.Text != "")
            {
                string[] diziStok = txtListStok.Text.Split(char.Parse(";"));
                details.KayitSil("DELETE FROM PROMODETAILS WHERE (prod_kod = N'" + details.Kod + "') AND (prod_tip = 1)");
                for (int i = 0; i < diziStok.Length; i++)
                {
                    if (diziStok[i].ToString() != "")
                    {
                        details.Satir = i;
                        details.PromoKod = diziStok[i].ToString();
                        details.Save();
                    }
                }
            }
            else
                details.KayitSil("DELETE FROM PROMODETAILS WHERE (prod_kod = N'" + details.Kod + "') AND (prod_tip = 1)");
        }
        details.Tipi = 2;
        if ((drpPromosyonTip.SelectedItem.Value == "5"))
        {
            if ((txtListReyon.Text != ""))
            {
                string[] diziReyon = txtListReyon.Text.Split(char.Parse(";"));
                details.KayitSil("DELETE FROM PROMODETAILS WHERE (prod_kod = N'" + details.Kod + "') AND (prod_tip = 2)");
                for (int i = 0; i < diziReyon.Length; i++)
                {
                    if (diziReyon[i].ToString() != "")
                    {
                        details.Satir = i;
                        details.PromoKod = diziReyon[i].ToString();
                        details.Save();
                    }
                }
            }
            else
                details.KayitSil("DELETE FROM PROMODETAILS WHERE (prod_kod = N'" + details.Kod + "') AND (prod_tip = 2)");
        }
        ClearArea();
    }
    private void Menu()
    {
        if (drpPromosyonTip.SelectedItem.Value == "0")
        {
            txtAlisverisLimiti.Enabled = true;
            txtIndirimOrani.Enabled = true;
            txtIndirimTutari.Enabled = true;
            drpOdemeTipi.Enabled = true;
            txtUrunLimit.Enabled = false;
            txtPromosyonAdeti.Enabled = false;
            txtUrunFiyat.Enabled = false;
            txtVerilecekUrun.Enabled = false;
            imgEkleVerilecekUrun.Visible = false;
            drpMstIndOran.Enabled = false;
        }
        else if (drpPromosyonTip.SelectedItem.Value == "1")
        {
            txtAlisverisLimiti.Enabled = false;
            txtIndirimOrani.Enabled = true;
            txtIndirimTutari.Enabled = true;
            drpOdemeTipi.Enabled = false;
            txtUrunLimit.Enabled = true;
            txtPromosyonAdeti.Enabled = true;
            txtUrunFiyat.Enabled = false;
            txtVerilecekUrun.Enabled = false;
            imgEkleVerilecekUrun.Visible = false;
            drpMstIndOran.Enabled = false;
        }
        else if (drpPromosyonTip.SelectedItem.Value == "2")
        {
            txtAlisverisLimiti.Enabled = false;
            txtIndirimOrani.Enabled = false;
            txtIndirimTutari.Enabled = false;
            drpOdemeTipi.Enabled = false;
            txtUrunLimit.Enabled = true;
            txtPromosyonAdeti.Enabled = true;
            txtUrunFiyat.Enabled = true;
            txtVerilecekUrun.Enabled = true;
            imgEkleVerilecekUrun.Visible = true;
            drpMstIndOran.Enabled = false;
        }
        else if (drpPromosyonTip.SelectedItem.Value == "3")
        {
            txtAlisverisLimiti.Enabled = true;
            txtIndirimOrani.Enabled = false;
            txtIndirimTutari.Enabled = false;
            drpOdemeTipi.Enabled = false;
            txtUrunLimit.Enabled = false;
            txtPromosyonAdeti.Enabled = true;
            txtUrunFiyat.Enabled = true;
            txtVerilecekUrun.Enabled = true;
            imgEkleVerilecekUrun.Visible = true;
            drpMstIndOran.Enabled = false;
        }
        else if (drpPromosyonTip.SelectedItem.Value == "4")
        {
            txtAlisverisLimiti.Enabled = true;
            txtIndirimOrani.Enabled = true;
            txtIndirimTutari.Enabled = false;
            drpOdemeTipi.Enabled = true;
            txtUrunLimit.Enabled = false;
            txtPromosyonAdeti.Enabled = false;
            txtUrunFiyat.Enabled = false;
            txtVerilecekUrun.Enabled = false;
            imgEkleVerilecekUrun.Visible = false;
            drpMstIndOran.Enabled = false;
        }
        else if (drpPromosyonTip.SelectedItem.Value == "5")
        {
            txtAlisverisLimiti.Enabled = false;
            txtIndirimOrani.Enabled = true;
            txtIndirimTutari.Enabled = true;
            drpOdemeTipi.Enabled = false;
            txtUrunLimit.Enabled = false;
            txtPromosyonAdeti.Enabled = false;
            txtUrunFiyat.Enabled = false;
            txtVerilecekUrun.Enabled = false;
            imgEkleVerilecekUrun.Visible = false;
            drpMstIndOran.Enabled = true;
        }
    }
    #endregion

    #region Kayıt Bul
    private void KayitBul(string id)
    {
        header = new PromoHeader();
        details = new PromoDetails();
        if (id != "")
        {
            header.Kod = id;
            header.KayitBul();
            txtPromosyonAdi.Text = header.Adi;
            txtAlisverisLimiti.Text = header.AlisverisLimit.ToString();
            txtIndirimOrani.Text = header.IndirimOrani.ToString();
            txtIndirimTutari.Text = header.IndirimTutari.ToString();
            txtPromosyonKod.Text = header.Kod;
            drpOdemeTipi.SelectedValue = header.OdemeTipi.ToString();
            txtPromosyonAdeti.Text = header.PromoAdet.ToString();
            txtUrunFiyat.Text = header.PromoFiyat.ToString();
            txtUrunLimit.Text = header.UrunLimit.ToString();
            txtVerilecekUrun.Text = header.PromoUrun;
            txtBaslangicTarihi.Text = Convert.ToString(header.BaslangicTarihi);
            txtBitisTarihi.Text = Convert.ToString(header.BitisTarihi);
            drpPromosyonTip.SelectedValue = header.Cinsi.ToString();
            drpIndirimTipi.SelectedValue = header.Tip.ToString();
            if (header.MusIndDahil == true)
                drpMstIndOran.SelectedValue = "0";
            else
                drpMstIndOran.SelectedValue = "1";
            DataTable dt = details.GetAllRecord_Datatable2("SELECT PROMODETAILS.prod_kod, PROMODETAILS.prod_satir, PROMODETAILS.prod_tip, PROMODETAILS.prod_promokod FROM PROMODETAILS WHERE PROMODETAILS.prod_kod ='" + header.Kod + "'");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][2].ToString() == "0")
                {
                    if (dt.Rows[i][3].ToString() != "")
                        txtList.Text += dt.Rows[i][3].ToString() + ";";
                }
                else if (dt.Rows[i][2].ToString() == "1")
                {
                    if (dt.Rows[i][2].ToString() != "")
                        txtListStok.Text += dt.Rows[i][3].ToString() + ";";
                }
                else if (dt.Rows[i][2].ToString() == "2")
                {
                    if (dt.Rows[i][2].ToString() != "")
                        txtListReyon.Text += dt.Rows[i][3].ToString() + ";";
                }
            }
            if (txtList.Text.Length > 0)
                txtList.Text = txtList.Text.Remove(txtList.Text.Length - 1, 1);
            if (txtListStok.Text.Length > 0)
                txtListStok.Text = txtListStok.Text.Remove(txtListStok.Text.Length - 1, 1);
            if (txtListReyon.Text.Length > 0)
                txtListReyon.Text = txtListReyon.Text.Remove(txtListReyon.Text.Length - 1, 1);
        }
    }
    #endregion

    #region Kayit Sil
    private void KayitSil(string id)
    {
        header = new PromoHeader();
        details = new PromoDetails();
        if (id != "")
        {
            header.Kod = id;
            header.KayitSil();
            details.KayitSil("DELETE  FROM  PROMODETAILS where prod_kod='" + id + "'");

        }
    }
    #endregion

    #region Attribute Add
    private void AttributeAdd()
    {
        imgEkle.Attributes.Add("onclick", "openpage('check=true&ctrl=" + txtList.ID + "&ltype=0');");
        imgEkle2.Attributes.Add("onclick", "openpage('check=true&ctrl=" + txtListStok.ID + "&ltype=1');");
        imgEkleReyon.Attributes.Add("onclick", "openpage('check=true&ctrl=" + txtListReyon.ClientID + "&ltype=2');");
        imgEkleVerilecekUrun.Attributes.Add("onclick", "openpage ('check=false&ctrl=" + txtVerilecekUrun.ClientID + "&VUrun=3');");
        imgbtnKaydet.Attributes.Add("onclick", "return Kontrol();");
        imgbtnKaydetKapat.Attributes.Add("onclick", "return Kontrol();");
        imgbtnSil.Attributes.Add("onclick", "return SilKontrol();");
        txtIndirimOrani.Attributes.Add("onblur", "DisableControl('" + txtIndirimOrani.ID + "','" + txtIndirimTutari.ID + "');");
        txtIndirimTutari.Attributes.Add("onblur", "DisableControl('" + txtIndirimTutari.ID + "','" + txtIndirimOrani.ID + "');");
    }
    #endregion

    #region ClearArea
    private void ClearArea()
    {
        for (int i = 0; i < MenuItemPromosyon.PageViews.Count; i++)
        {
            foreach (Control c in MenuItemPromosyon.PageViews[i].Controls)
            {
                if (c is CrmControls.CrmTextBox)
                {
                    CrmControls.CrmTextBox textbox = c as CrmControls.CrmTextBox;
                    textbox.Text = "";
                }
                if (c is CrmControls.CrmDropDown)
                {
                    CrmControls.CrmDropDown drop = c as CrmControls.CrmDropDown;
                    if (drop.SelectedIndex != -1)
                        drop.SelectedIndex = 0;
                }
            }
        }

    }
    #endregion

    #region Fields
    PromoHeader header;
    PromoDetails details;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString.Count > 0)
            {
                KayitBul(Request.QueryString["id"]);
            }
        }
        AttributeAdd();
        Menu();

    }

    protected void imgbtnKaydet_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.ClientScript.RegisterStartupScript(typeof(Page), "Tazele", "Refresh();", true);
    }

    protected void imgbtnKaydetKapat_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.ClientScript.RegisterStartupScript(typeof(Page), "Close", "PageClose();", true);
    }

    protected void imgbtnSil_Click(object sender, ImageClickEventArgs e)
    {
        if (txtPromosyonKod.Text != "")
        {
            KayitSil(txtPromosyonKod.Text);
            ClearArea();
            this.ClientScript.RegisterStartupScript(typeof(Page), "Refresh", "Refresh();", true);
        }
    }

    protected void imgNewREcord_Click(object sender, ImageClickEventArgs e)
    {
        ClearArea();
    }
}
